<?
class Upload_model extends CI_Model {

	var $gallery_path;
	var $gallery_path_url;

	function Upload_model()
	{
		parent::__construct();
		
		$this->gallery_path = realpath(APPPATH . '../galleri');
		$this->gallery_path_url = base_url() . 'galleri/';
	}

	function do_upload($deltager_id,$titel)
	{
		$config = array
		(
			'allowed_types' => 'jpg|jpeg|gif|png|psd|pdf',
			'upload_path' => $this->gallery_path,

			//'max_size' => 10000
		);

		$this->load->library('upload', $config);
		$this->upload->do_upload();
		$image_data = $this->upload->data();

	print_r($image_data);

		$config = array
		(
			'source_image' => $image_data['full_path'],
			'new_image' => $this->gallery_path . '/thumbs',
			'maintain_ration' => true,
			'width' => 150,
			'height' => 100
		);
			echo $image_data['file_name'];

		$datestring = "%Y-%m-%d %H:%i:%s";
		$time = time();
		$upload_time = mdate($datestring, $time);

		$data_til_db_billede = array(

			'url' => $image_data['file_name'],
			'titel' => $titel, 
			'created' => $upload_time,
			'id_deltager' => $deltager_id		
		);
		$this->db->insert('billede', $data_til_db_billede);	

		// Nu hentes det oprettede id ud igen fra billedetabellen.
		$query = $this->db->query('SELECT * FROM billede ORDER BY id DESC LIMIT 1');
		$row = $query->row();
		$billede_id = $row->id;


		$this->load->library('image_lib', $config);
		$this->image_lib->resize();
		
		return $billede_id;	
	}
	
	function billede_relation($billede_id, $element_id)
	{

		$data = array(
			'id_billede' => $billede_id,
			'id_element' => $element_id,
			'primaer' => 1		
		);
		$this->db->insert('billede_relation', $data);
	}

	function get_images ()
	{
		$files = scandir($this->gallery_path);
		$files = array_diff($files, array('.','..', 'thumbs'));		

		$images = array();

		foreach ($files as $file)
		{
			$images []= array(
				'url' => $this->gallery_path_url . $file,
				'thumb_url' => $this->gallery_path_url . 'thumbs/' . $file,
			);
			
		
		}

		return $images;

	}




}

?>
